<?php
namespace app\admin\controller;

use think\App;
use app\BaseController;
use think\facade\View;
use app\Request;
use app\repositories\contracts\MenuInterface;

/***
 * Class ViewBaseController
 * @package app\admin\controller
 * @Author Vee7
 * @Date 2021/12/14
 * @Time 13:07
 * @Description 用于统一渲染公共布局的数据，后台所有需要渲染视图的控制器都应该继承本类。
 */
class ViewBaseController extends BaseController
{
    public $app;
    protected $menu;
    protected $rootData;
    protected $promise_menu_id;
    protected $menuData;

    public function __construct(App $app,MenuInterface $menu,Request $request)
    {
        $this->app = $app;
        $this->menu = $menu;
        $this->rootData = $request->rootData;
        $this->promise_menu_id = $request->promise_menu_id;
        $this->menuData = $request->menuData;
        parent::__construct($this->app);
        // 控制器初始化
        $this->Viewinit();
    }

    public function Viewinit()
    {
        //菜单递归按父子关系重组为多维数组
        $menu_list = $this->menu->getList();
        //公共方法实现树形菜单数据
        //halt(treeMenu($this->menu->getMenuList()));
        //面包屑导航
        //halt($this->menuData);
        if($this->menuData['id']){
            $menuAllParent = $this->menu->getAllParent($this->menuData['id']);
            $menuAllParent = array_sort($menuAllParent,'parent_id','asc');
        }else{
            $menuAllParent = [];
        }

        View::assign([
            'layout_menu_list'  => $menu_list,
            'layout_menu_list_json'  => json_encode($menu_list),
            'rootData' => $this->rootData,
            'promise_menu_id' => $this->promise_menu_id,
            'menuAllParent' => $menuAllParent,
        ]);
    }
}
